# http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-fields/
# http://docs.djangoproject.com/en/dev/topics/db/queries/#queryset-model-example
#http://docs.djangoproject.com/en/dev/topics/files/
from django.db import models
from django import forms
from django.template.defaultfilters import slugify
from datetime import datetime

class File(models.Model):
    name = models.CharField(max_length=255)
    file_type = models.CharField(max_length=5)
    resource = models.FileField(upload_to='files')
    unit = models.ForeignKey('Unit')
    
    def __unicode__(self):
        return  '%s' %(self.resource)    
        
class Employee(models.Model):
    name_first = models.CharField(max_length=100)
    name_last = models.CharField(max_length=100)
    email  = models.EmailField(max_length=100)
    job_title = models.CharField(max_length=100)
    phone = models.IntegerField()
    
    def __unicode__(self):
        return  '%s, %s' %(self.name_last, self.name_first)

    def get_absolute_url(self):
        # site = Site.objects.get_current().domain
        #host = gethostname()
        return '/staff/%i/' % self.id

    class Meta:
        #ordering = ['-name_last']
    	ordering = ['name_last']

class Page(models.Model):
    title = models.CharField(max_length=100)
    slug  = models.SlugField(editable=False) # hide from admin
    body_text = models.TextField()
    created = models.DateField(editable=False)
    updated = models.DateTimeField(editable=False)
    unit = models.ForeignKey('Unit')
    
    def __unicode__(self):
        return '%s - %s'%(self.unit,self.title)
            
    def save(self):
        if not self.id:
            self.created = datetime.today()
            self.slug = slugify( self.title )
        self.updated = datetime.today()
        super(Page, self).save()

    def get_absolute_url(self):
        # site = Site.objects.get_current().domain
        #host = gethostname()
        return '/%s/%s/' % (self.unit.slug,self.slug)


class Unit(models.Model):
    title = models.CharField(max_length=100)
    slug  = models.SlugField(editable=False) # hide from admin
    vision_statement = models.TextField()
    employees = models.ManyToManyField(Employee)

    def __unicode__(self):
        return self.title

    def save(self):
        self.slug = slugify( self.title )
        super(Unit, self).save()        

    def get_absolute_url(self):
        # site = Site.objects.get_current().domain
        #host = gethostname()
        
        #return '/%s/' % self.title
        return '/%s/' % self.slug
    
